package controller;

import com.google.gson.Gson;
import pojo.Book_info;
import service.IBookService;
import service.impl.BookServiceImpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/book/*")
public class BookController extends HttpServlet {
    IBookService ibs=new BookServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        if (mName.length() == 0) {
            mName = "showdt";
        }
        Class cla = this.getClass();
        Method method = null;
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        String user_code=request.getParameter("账号");
        String password=request.getParameter("密码");
        if ("admin".equals(user_code)&&"1234".equals(password)) {  // 如果登录成功（返回的Emp对象不为空）
            HttpSession session = request.getSession();
            session.setAttribute("user", user_code);
            response.sendRedirect(request.getContextPath() + "/bookshow.jsp");
        } else {  // 如果登录失败
            request.setAttribute("error", "用户名或密码错误");
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
    }
    public void showdt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");

        String type = request.getParameter("book_type");

        int book_type = -1;
        if (type != null && !type.trim().isEmpty() && !"-1".equals(type)) {
            try {
                book_type = Integer.parseInt(type.trim());
            } catch (NumberFormatException e) {
                book_type = -1;
            }
        } else {
            System.out.println("参数为空或为-1，使用默认值-1");
        }

        // 测试数据库连接和查询
        int allCount = ibs.countQueryByType(-1);

        int count = ibs.countQueryByType(book_type);

        PageHelper ph = new PageHelper();
        ph.setDateCount(count);

        String indexpage = request.getParameter("indexpage");
        if (indexpage == null || indexpage.trim().isEmpty()) {
            ph.setPageIndex(1);
        } else {
            try {
                ph.setPageIndex(Integer.parseInt(indexpage.trim()));
            } catch (NumberFormatException e) {
                ph.setPageIndex(1);
            }
        }

        ArrayList<Book_info> books = ibs.queryByType(book_type, ph);

        if (books != null && !books.isEmpty()) {
            for (Book_info book : books) {
                System.out.println("图书: " + book.getBook_name() + ", ID: " + book.getBook_id());
            }
        }

        ph.setListIndex(books != null ? books : new ArrayList<>());

        String json = new Gson().toJson(ph);

        response.getWriter().println(json);
    }
    public void showdtByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
    }
    public void showdtByBorrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
    }
}




